package com.zzc.controller;

import com.zzc.domain.Admin;
import com.zzc.service.impl.AdminServiceImp;
import com.zzc.util.SendMail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.List;
import java.util.Random;

@Controller
public class LoginController {

    @Autowired
    AdminServiceImp adminServiceImp;

    @Autowired
    SendMail sendMail;

    @PostMapping("/goin")
    public String Login(String email, String password, boolean remember, Model model, HttpServletRequest request, HttpServletResponse response){
        Admin admin = adminServiceImp.loginAuthentication(email);
        if (admin==null){
            model.addAttribute("msg","账号或密码错误");
            return "login";
        }else if (!admin.getPwd().equals(DigestUtils.md5DigestAsHex(password.getBytes()))){
            model.addAttribute("msg","账号或密码错误");
            return "login";
        }else {
            Enumeration em = request.getSession().getAttributeNames();
            while(em.hasMoreElements()){
                request.getSession().removeAttribute(em.nextElement().toString());
            }
            request.getSession().setAttribute("username", "Zzc");
            if (remember){
                Cookie cookie = new Cookie("email",email);
                cookie.setMaxAge(2592000);
                Cookie cookie1 = new Cookie("password",DigestUtils.md5DigestAsHex(password.getBytes()));
                cookie1.setMaxAge(2592000);
                response.addCookie(cookie);
                response.addCookie(cookie1);
            }
            return "redirect:/index";
        }

    }
    @GetMapping("/login")
    public String toLogin(){
        return "login";
    }

    @GetMapping({"/","/index"})
    public String index(){
        return "index";
    }

    @GetMapping("/events")
    public String getEvents(){
        return "events";
    }

    @GetMapping("/positioning")
    public String posit(){
        return "positioning";
    }

    @GetMapping("/logout")
    public  String logout(HttpServletRequest request,HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        if (cookies!=null){
            for (Cookie cookie : cookies) {
                cookie.setMaxAge(0);
                cookie.setPath("/");
                response.addCookie(cookie);
            }
        }

        Enumeration em = request.getSession().getAttributeNames();
        while(em.hasMoreElements()){
            request.getSession().removeAttribute(em.nextElement().toString());
        }
        return "login";
    }

    @GetMapping("/lock")
    public String lock(HttpServletRequest request,HttpServletResponse response){
        Enumeration em = request.getSession().getAttributeNames();
        while(em.hasMoreElements()){
            request.getSession().removeAttribute(em.nextElement().toString());
        }
        request.getSession().setAttribute("username", "lock");
        return "lock";
    }

    @PostMapping("/unlock")
    public String unLock(String password,HttpServletRequest request){
        List<Admin> admin = adminServiceImp.getAdmin();

        if (DigestUtils.md5DigestAsHex(password.getBytes()).equals(admin.get(0).getPwd())){
            Enumeration em = request.getSession().getAttributeNames();
            while(em.hasMoreElements()){
                request.getSession().removeAttribute(em.nextElement().toString());
            }
            request.getSession().setAttribute("username", "Zzc");
        }
        return "redirect:/index";
    }

    @GetMapping("/recovery")
    public String recovery(){
        return "pwdRecovery";
    }

    @PostMapping("/pwdrecovery")
    public String pwdRec(String mail,Model model){

        String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i <7 ; i++) {
            sb.append(allChar.charAt(random.nextInt(allChar.length())));
        }
        adminServiceImp.pwdRecovery(DigestUtils.md5DigestAsHex(sb.toString().getBytes()),mail);
        sendMail.pwdRecovery(sb.toString(),mail);
        model.addAttribute("msg","重置密码已发送到邮箱");
        return "/login";
    }
}
